package com.superyee.commonlib.utils;

import android.content.Context;
import android.widget.TextView;

import androidx.appcompat.widget.SearchView;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.superyee.commonlib.R;

import java.lang.reflect.Field;

/**
 * Created by guoguo on 2017/11/21.
 */

public class ViewUtils {


    public static void initRefresh(SwipeRefreshLayout mRefreshLayout) {
        mRefreshLayout.setProgressBackgroundColorSchemeColor(ContextCompat.getColor(mRefreshLayout.getContext(), R.color.colorAccent));
        mRefreshLayout.setColorSchemeResources(android.R.color.white, R.color.white,
                R.color.colorPrimaryDark, R.color.yellow);
    }

    /**
     * 初始化recyclerview内容为cardView时样式
     *
     * @param context
     * @param listView
     */
    public static void initRecyclerViewCardView(Context context, RecyclerView listView) {
        listView.setHasFixedSize(true);
        int padding = context.getResources().getDimensionPixelSize(R.dimen.dp_5);
        listView.setPadding(padding, padding, padding, padding);
        listView.setClipToPadding(false);
        listView.setLayoutManager(new WrapLinearLayoutManager(context));
        listView.addItemDecoration(SpaceItemDecorationUtils.getHeightSpace(context, context.getResources().getDimensionPixelSize(R.dimen.dp_2)));
    }

    public static void initSearchView(SearchView searchView) {
        initSearchView(searchView, "");
    }

    public static void initSearchView(SearchView searchView, String hint) {
        try {
            Class cls = Class.forName("androidx.appcompat.widget.SearchView");
            Field field = cls.getDeclaredField("mSearchSrcTextView");
            field.setAccessible(true);
            TextView tv = (TextView) field.get(searchView);
            Class[] clses = cls.getDeclaredClasses();
            for (Class cls_ : clses) {
                if (cls_.toString().endsWith("androidx.appcompat.widget.SearchView$SearchAutoComplete")) {
                    Class targetCls = cls_.getSuperclass().getSuperclass().getSuperclass().getSuperclass();
                    Field cuosorIconField = targetCls.getDeclaredField("mCursorDrawableRes");
                    cuosorIconField.setAccessible(true);
                    cuosorIconField.set(tv, R.drawable.comm_cursor);
                    break;
                }
            }
        } catch (Exception e) {

        }
//        ImageView icon = searchView.findViewById(android.support.v7.appcompat.R.id.search_button);
//        if (icon != null) {
//            icon.setColorFilter(Color.WHITE);
//        }
//        icon = searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
//        if (icon != null) {
//            icon.setColorFilter(Color.WHITE);
//        }
        SearchView.SearchAutoComplete textView = searchView.findViewById(androidx.appcompat.R.id.search_src_text);
        if (textView != null) {
//            textView.setTextColor(Color.WHITE);
//            textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
//            textView.setHintTextColor(ContextCompat.getColor(textView.getContext(), R.color.grey_cbd0d8));
            textView.setHint(hint);
        }
    }

}
